<?php

namespace StudyBuddy;

class UserTagsBlock extends StudyBuddyObject {
    /**
     * Maximum tags to show in user tags block
     * A very active user can have hundreds of tags, we
     * want to show only the 60
     * most popular tags for this user
     *
     *
     * @var int
     */
    const MAX_TO_SHOW = 60;

    /**
     *
     * Renders block with user tags
     *
     * @todo get Viewer from Registry and if NOT
     * the same as oUser then get array intersection
     * and show you have these 'tags' in common
     *
     * @param object $oRegistry Registry object
     * @param object $oUser User object
     */
    public static function get(Registry $oRegistry, User $oUser) {
        $uid = $oUser->getUid();
        $aTags = $oRegistry->Mongo->USER_TAGS->findOne(array('_id' => $uid));

        if (empty($aTags) || empty($aTags['tags'])) {
            d('no tags for user: ' . $uid);

            return '';
        }

        $aUserTags = $aTags['tags'];
        d('$aUserTags: ' . print_r($aUserTags, 1));

        $count = count($aUserTags);

        /**
         * @todo Translate string
         */
        $blockTitle = "User's most active tags";
        if ($count > self::MAX_TO_SHOW) {
            $aUserTags = \array_slice($aUserTags, 0, self::MAX_TO_SHOW);
        }

        $tags = '';
        foreach ($aUserTags as $tag => $count) {
            $tags .= \tplUserTag::parse(array($tag, $count), false);
        }

        d('tags: ' . $tags);

        /**
         * @todo translate string
         */
        $vals = array('count' => $blockTitle, 'label' => 'tag', 'tags' => $tags);
        d('vals: ' . print_r($vals, 1));

        $ret = \tplUserTags::parse($vals);

        d('ret: ' . $ret);

        return $ret;
    }

    /**
     * @todo finish this
     *
     * Finds and parses common tags a Viewer has
     * with User whos profile user is viewing
     *
     * @param User $oViewer
     * @param array $userTags
     */
    public static function getCommonTags(User $oViewer, array $userTags) {

        $uid = $oViewer->getUid();
        if (0 === $uid) {
            return '';
        }

        $aTags = $oRegistry->Mongo->USER_TAGS->findOne(array('_id' => $uid));

        if (empty($aTags) || empty($aTags['tags'])) {
            d('no tags for user: ' . $uid);

            return '';
        }

        $viewerTags = $aTags['tags'];

        $aCommon = array_intersect_key($viewerTags, $userTags);
        d('aCommon: ' . print_r($aCommon, 1));

        if (empty($aCommon)) {
            d('no common tags');

            return '';
        }
    }

}
